package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SalaryRecordMapper;
import com.ruoyi.system.domain.SalaryRecord;
import com.ruoyi.system.service.ISalaryRecordService;

/**
 * 薪资记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-17
 */
@Service
public class SalaryRecordServiceImpl implements ISalaryRecordService 
{
    @Autowired
    private SalaryRecordMapper salaryRecordMapper;


    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询薪资记录
     * 
     * @param id 薪资记录主键
     * @return 薪资记录
     */
    @Override
    public SalaryRecord selectSalaryRecordById(Long id)
    {
        return salaryRecordMapper.selectSalaryRecordById(id);
    }

    /**
     * 查询薪资记录列表
     * 
     * @param salaryRecord 薪资记录
     * @return 薪资记录
     */
    @Override
    public List<SalaryRecord> selectSalaryRecordList(SalaryRecord salaryRecord)
    {
        //薪资发放记录
        //对于除了学校工作人员或者管理员其余只能看到自己的薪资记录
        if(SecurityUtils.hasRole("school") || SecurityUtils.hasRole("admin")){
            return salaryRecordMapper.selectSalaryRecordList(salaryRecord);
        }
        salaryRecord.setUserId(SecurityUtils.getUserId());
        return salaryRecordMapper.selectSalaryRecordList(salaryRecord);

    }

    /**
     * 新增薪资记录
     * 
     * @param salaryRecord 薪资记录
     * @return 结果
     */
    @Override
    public int insertSalaryRecord(SalaryRecord salaryRecord)
    {
        salaryRecord.setCreateTime(DateUtils.getNowDate());
        Long userId = salaryRecord.getUserId();
        SysUser sysUser = userMapper.selectUserById(userId);
        salaryRecord.setAccount(sysUser.getAccount());
        //获取当前月份
        LocalDate currentDate = LocalDate.now();
        Month month = currentDate.getMonth();
        String monthName = month.getDisplayName(TextStyle.FULL, Locale.CHINESE);

        salaryRecord.setMoney(salaryRecord.getMoney());
        salaryRecord.setTitle(sysUser.getNickName()+monthName+"工资");
        return salaryRecordMapper.insertSalaryRecord(salaryRecord);
    }

    /**
     * 修改薪资记录
     * 
     * @param salaryRecord 薪资记录
     * @return 结果
     */
    @Override
    public int updateSalaryRecord(SalaryRecord salaryRecord)
    {
        salaryRecord.setUpdateTime(DateUtils.getNowDate());
        return salaryRecordMapper.updateSalaryRecord(salaryRecord);
    }

    /**
     * 批量删除薪资记录
     * 
     * @param ids 需要删除的薪资记录主键
     * @return 结果
     */
    @Override
    public int deleteSalaryRecordByIds(Long[] ids)
    {
        return salaryRecordMapper.deleteSalaryRecordByIds(ids);
    }

    /**
     * 删除薪资记录信息
     * 
     * @param id 薪资记录主键
     * @return 结果
     */
    @Override
    public int deleteSalaryRecordById(Long id)
    {
        return salaryRecordMapper.deleteSalaryRecordById(id);
    }
}
